package com.study.fetch;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.junit.jupiter.api.Test;

import javax.annotation.Nonnull;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author zhuzi
 * 2024/4/3 14:43
 */
public class FetchJiuYang {
    /**
     * 涨停简图
     */
    @Test
    public void zhangtinghjiantu() {
        String url = "https://app.jiuyangongshe.com/jystock-app/api/v1/action/diagram-url";

        String reqBodyFormat = "{\"date\":\"{}\"}";


        String fastSimpleUUID = IdUtil.fastSimpleUUID();
        System.out.println(fastSimpleUUID);
        Map<String, String> headerMap = MapUtil.<String, String>builder()//
                .put("Timestamp", System.currentTimeMillis() + "")//
                .put("Token", fastSimpleUUID)//
                .put("Platform", "2").build();

        String body = HttpRequest.post(url).headerMap(headerMap, true).body(StrUtil.format(reqBodyFormat, LocalDate.now())).execute().body();
        System.out.println(body);

    }

    interface JiuYang {

        LoadingCache<String, String> transCache = CacheBuilder.newBuilder()
                // 设置并发级别为3，并发级别是指可以同时写缓存的线程数
                .concurrencyLevel(3)
                // 过期
                .expireAfterWrite(1, TimeUnit.DAYS)
                // 初始容量
                .initialCapacity(100)
                // 最大容量，超过LRU
                .maximumSize(200).build(new CacheLoader<String, String>() {
                    @Override
                    public String load(@Nonnull String key) {
                        return IdUtil.fastSimpleUUID();
                    }
                });

        interface Const {
            /**
             * 涨停分析 https://app.jiuyangongshe.com/jystock-app/api/v1/action/diagram-url
             */
            String urlZtfx = "https://app.jiuyangongshe.com/jystock-app/api/v1/action/diagram-url";
        }

        class JYUtils {
            public static String getZtfx() {
                Map<String, String> headerMap = MapUtil.<String, String>builder()//
                        .put("Timestamp", System.currentTimeMillis() + "")//
                        .put("Token", transCache.getUnchecked(LocalDate.now().toString()))//
                        .put("Platform", "2").build();
                String reqBodyFormat = "{\"date\":\"{}\"}";
                String body = HttpRequest.post(Const.urlZtfx).headerMap(headerMap, true).body(StrUtil.format(reqBodyFormat, LocalDate.now())).execute().body();
                try {
                    String data = JSON.parseObject(body).getString("data");
                    return StrUtil.format("🎉 韭妍公社消息\n\n" + "🏆 涨停分析\n\n" + "📌 {}\n{}", data, LocalDateTime.now());
                } catch (Exception e) {
                }
                return StrUtil.EMPTY;
            }
        }
    }

}
